﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoanPrequalificationSystem.Model.Borrowers
{
    public class Employer
    {
        private float _monthlyIncome;
        private IAddress _address;
        private string _name;
        public Employer()
        {

        }

        public Employer(float monthlyIncome, IAddress EmployerAddress, string EmployerName)
        {
            _monthlyIncome = monthlyIncome;
            _address = EmployerAddress;
            _name = EmployerName;

            if (GetBrokenRules().Count > 0)
            {
                throw new Exception(String.Format("The Employer you are trying to create contains invalid data.{0}", GetBrokenRules()));
            }
        }
        public List<BrokenBusinessRule> GetBrokenRules()
        {
            var brokenRules = new List<BrokenBusinessRule>();

            if (string.IsNullOrEmpty(Name))
            {
                brokenRules.Add(new BrokenBusinessRule("Name", "a name must defined for an employer"));
            }
            if (MonthlyIncome < 0)
            {
                brokenRules.Add(new BrokenBusinessRule("MonthlyIncome", "Monthly Income must be greater than 0."));
            }
            if (Address == null)
            {
                brokenRules.Add(new BrokenBusinessRule("Address", "An Address must defined for an employer"));
            }
            else if (Address.GetBrokenRules().Count > 0)
            {
                AddToBrokenRulesList(brokenRules, Address.GetBrokenRules());
            }
            return brokenRules;
        }

        private void AddToBrokenRulesList(List<BrokenBusinessRule> currentBrokenRules, List<BrokenBusinessRule> brokenRulesToAdd)
        {
            foreach (BrokenBusinessRule brokenrules in brokenRulesToAdd)
            {
                currentBrokenRules.Add(brokenrules);
            }
        }

        public float MonthlyIncome
        {
            get
            {
                return _monthlyIncome;
            }
            set
            {
                _monthlyIncome = value;
            }
        }
        public IAddress Address
        {
            get
            {
                return _address;
            }
            set
            {
                _address = value;
            }
        }
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
    }
}
